package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Community;
import com.asm.repository.CommunityRepository;
import com.asm.repository.TradeAreaRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.CommunityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author morningbear
 */
@Service
@Slf4j
public class CommunityService {
    @Resource
    private CommunityRepository communityRepository;

    @Resource
    private TradeAreaRepository tradeAreaRepository;

    @Resource
    private DistrictService districtService;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    private PageResult<CommunityVo> getCommunityVoPageResult(Integer pageSize, Integer pageNumber, Page<Community> page) {
        List<CommunityVo> communityList = new ArrayList<>();
        for (Community community : page.getContent()) {
            CommunityVo communityVo = new CommunityVo();
            BeanUtils.copyProperties(community, communityVo);
            communityVo.setTradeArea(tradeAreaRepository.findById(community.getTradeAreaId()).get().getTradeAreaName());
            communityList.add(communityVo);
        }

        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), communityList);
    }

    /**
     * 查询所有小区
     *
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findAllCommunities(Integer pageNumber, Integer pageSize) {
        Page<Community> page = communityRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     * 根据小区类型查找小区
     *
     * @param type 小区类型
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityType(String type, Integer pageNumber, Integer pageSize) {
        Page<Community> page = communityRepository.findByTypeAndIsDel(type, false, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     * 根据小区名字查找小区
     *
     * @param name 小区名字
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityName(String name, Integer pageNumber, Integer pageSize) {
        Page<Community> page = communityRepository.findByNameAndIsDel(name, false, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

//    /**
//     *根据小区地址查找小区
//     * @param address String
//     * @return List<Map<String, Object>>
//     */
//    public List<Map<String, Object>> findByCommunityAddress(Point address){
//        List<Community> communityList = communityRepository.findByAddress(address);
//        List<Map<String, Object>> results = new ArrayList<>();
//        for (Community community : communityList){
//            results.add(getCommunityResult(community));
//        }
//        return results;
//    }

    /**
     * 根据镇/区编号查找小区
     *
     * @param districtId 镇/区编号
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityDistrictId(Long districtId, Integer pageNumber, Integer pageSize) {
        Page<Community> page = communityRepository.findByDistrictIdAndIsDel(districtId, false, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     * 根据商圈编号查找小区
     *
     * @param tradeAreaId 商圈编号
     * @return PageResult<CommunityVo>
     */
    public PageResult<CommunityVo> findByCommunityTradeAreaId(Long tradeAreaId, Integer pageNumber, Integer pageSize) {
        Page<Community> page = communityRepository.findByTradeAreaIdAndIsDel(tradeAreaId, false, PageRequest.of(pageNumber, pageSize));
        return getCommunityVoPageResult(pageSize, pageNumber, page);
    }

    /**
     * 根据编号查询小区
     *
     * @param id 小区编号
     * @return PageResult<CommunityVo>
     */
    public CommunityVo getCommunity(Long id) {
        CommunityVo communityVo = new CommunityVo();
        if (id == null) {
            return communityVo;
        } else {
            Community community = communityRepository.findCommunityById(id);
            if (community == null) {
                return communityVo;
            } else {
                BeanUtils.copyProperties(community, communityVo);
                return communityVo;
            }
        }
    }

    /**
     * 添加小区
     *
     * @param communityVo 小区
     * @return 社区id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addCommunity(CommunityVo communityVo) {
        Community community = new Community();
        BeanUtils.copyProperties(communityVo, community);
        community.setId(snowflakeIdWorker.nextId());
        community.setCreateAt(new Date());
        community.setCreateBy(1L);
        community.setUpdateAt(new Date());
        community.setUpdateBy(1L);
        communityRepository.save(community);
        return community.getId();
    }

    /**
     * 删除小区
     *
     * @param id 小区编号
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCommunity(Long id) {
        Community community = communityRepository.findById(id).get();
        community.setIsDel(true);
        community.setUpdateBy(1L);
        community.setUpdateAt(new Date());
        communityRepository.save(community);
        //communityRepository.deleteById(id);
    }

    /**
     * 更新小区
     *
     * @param communityVo 小区实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCommunity(CommunityVo communityVo) {
        Community community = communityRepository.findById(communityVo.getId()).get();
        BeanUtils.copyProperties(communityVo, community);
        community.setUpdateBy(1L);
        community.setUpdateAt(new Date());
        communityRepository.save(community);
    }

    /**
     * 返回小区的经纬度
     *
     * @param id 小区编号
     */
    public Point getCommunityAddress(Long id) {
        Community community = communityRepository.getOne(id);
        double longitude = Double.parseDouble(community.getLongitude());
        double latitude = Double.parseDouble(community.getLatitude());
        return new Point(longitude, latitude);
    }

    /**
     * 根据省份、城市和地区查询小区信息
     *
     * @param province 传入的参数
     * @param city     城市
     * @param district 地区
     * @return 小区列表
     */
    public List<CommunityVo> getCommunitiesByParams(String province, String city, String district) {
        List<Long> districtIds = districtService.getDistrictIds(province, city, district);
        List<CommunityVo> list = new ArrayList<>();
        for (Long id : districtIds) {
            PageResult<CommunityVo> page = this.findByCommunityDistrictId(id, 0, 1000);
            for (CommunityVo communityVo : page.getData()) {
                list.add(communityVo);
            }
        }
        return list;
    }

}
